<!DOCTYPE html>
<html>

<head>
    <title>列表渲染</title>

    <!-- 开发环境版本，包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

</head>

<body>
    <ul id="example-1">
        <li v-for="item in items">
            {{ item.message }}
        </li>
    </ul>

    <ul id="example-2">
        <li v-for="(item, index) in items">
            {{ parentMessage  }} - {{ index }} - {{ item.message }}
        </li>
    </ul>

    <!-- 在 v-for 里使用对象 -->
    <ul id="v-for-object" class="demo">
        <li v-for="value in object">
            {{ value }}
        </li>
        <hr>
        <li v-for="(value, name) in object">
            {{ name }}: {{ value }}
        </li>
        <hr>
        <li v-for="(value, name, index) in object">
            {{ index }}. {{ name }}: {{ value }}
        </li>
    </ul>


    <!-- 显示过滤/排序后的结果 -->
    <ul id="example-3">
        <li v-for="n in evenNumbers">{{ n }}</li>
        <hr>
        <li v-for="n in every(numbers)">{{ n }}</li>
    </ul>

    
    <!-- 在 v-for 里使用值范围 -->
    <div id="example-4">
        <span v-for="n in 10">{{ n }} </span>

        <!-- v-for 与 v-if 一同使用 -->
        <ul>
            <li v-for="todo in todos" v-if="!todo.isComplete">{{ todo }}</li>
        </ul>
    </div>


    <!-- 在组件上使用 v-for -->
    <div id="example-5">
        <my-component v-for="item in items" :a="item"></my-component>
    </div>


    <script>
        var example1 = new Vue({
            el: '#example-1',
            data: {
                items: [
                    { message: 'Foo' },
                    { message: 'Bar' }
                ]
            }
        });

        var example2 = new Vue({
            el: '#example-2',
            data: {
                parentMessage: 'Parent',
                items: [
                    { message: 'Foo' },
                    { message: 'Bar' }
                ]
            }
        });

        new Vue({
            el: '#v-for-object',
            data: {
                object: {
                    title: 'How to do lists in Vue',
                    author: 'Jane Doe',
                    publishedAt: '2016-04-10'
                }
            }
        });

        var example3 = new Vue({
            el: '#example-3',
            data: {
                numbers: [ 1, 2, 3, 4, 5 ]
            },
            computed: {
                evenNumbers: function() {
                    return this.numbers.filter(function(number) {
                        return number % 2 === 0;
                    });
                }
            },
            methods: {
                every: function (args) {
                    return args.filter(function(x) {
                        return x % 2 != 0;
                    });
                }
            }
        });

        new Vue({
            el: '#example-4',
            data: {
                todos: [
                    { isComplete: true, msg: "todo1" },
                    { isComplete: true, msg: "todo2" },
                    { isComplete: false, msg: "todo3" },
                    { isComplete: true, msg: "todo4" },
                    { isComplete: false, msg: "todo5" },
                ]
            }
        });

        Vue.component('my-component', {
            props: ['a'],
            template: "<h2>{{ a }}</h2>"
        });

        new Vue({
            el: '#example-5',
            data: {
                items: ['key1', 'key2', 'key3']
            }
        });
    </script>
</body>

</html>